package com.runen.infinitemovement.widget.popupwind;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.activity.SportsStatusActivity;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.rxnetlibrary.utils.PermissionUtils;
import com.runen.rxnetlibrary.utils.ToastUtils;
import com.runen.rxnetlibrary.utils.constant.PermissionConstants;
import com.runen.rxnetlibrary.utils.helper.DialogHelper;

import java.util.List;

public class SportsPoppupWindow extends PopupWindow {
    private ImageView iv_dismiss, iv_sports_run, iv_sports_walk, iv_sports_riding;
    private TextView tv_sports_run, tv_sports_walk, tv_sports_riding, tv_sure;
    private LinearLayout ll_sports_run, ll_sports_walk, ll_sports_riding;
    private Context context;
    private View layout;


    public static SportsPoppupWindow getInstance(Context context) {
        return new SportsPoppupWindow(context);
    }

    private SportsPoppupWindow(Context context) {
        super(context);
        this.context = context;
        initView();
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //this.setHeight((int) (CommonUtil.getScreenHW((Activity) context)[1] / 3));
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 这样设置才能点击屏幕外dismiss窗口
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(layout);

        initEvent();
    }

    private void initView() {
        layout = View.inflate(context, R.layout.popup_sports, null);
        iv_dismiss = layout.findViewById(R.id.iv_dismiss);

        tv_sports_run = layout.findViewById(R.id.tv_sports_run);
        tv_sports_walk = layout.findViewById(R.id.tv_sports_walk);
        tv_sports_riding = layout.findViewById(R.id.tv_sports_riding);
        tv_sure = layout.findViewById(R.id.tv_sure);

        iv_sports_run = layout.findViewById(R.id.iv_sports_run);
        iv_sports_walk = layout.findViewById(R.id.iv_sports_walk);
        iv_sports_riding = layout.findViewById(R.id.iv_sports_riding);

        ll_sports_run = layout.findViewById(R.id.ll_sports_run);
        ll_sports_walk = layout.findViewById(R.id.ll_sports_walk);
        ll_sports_riding = layout.findViewById(R.id.ll_sports_riding);

    }

    private int type;

    private void initEvent() {
        iv_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        //关闭Dialog
        ll_sports_run.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                type = 0;
                ll_sports_run.setBackgroundResource(R.drawable.bg_ee4238_df2526_360dp);
                tv_sports_run.setTextColor(CommonUtil.getColor(R.color.white));
                iv_sports_run.setImageResource(R.mipmap.ico_run_white);

                ll_sports_walk.setBackgroundResource(R.drawable.bg_ffeeeeee_360dp);
                tv_sports_walk.setTextColor(CommonUtil.getColor(R.color.color_FFB1B3C1));
                iv_sports_walk.setImageResource(R.mipmap.ico_walk_gray);

                ll_sports_riding.setBackgroundResource(R.drawable.bg_ffeeeeee_360dp);
                tv_sports_riding.setTextColor(CommonUtil.getColor(R.color.color_FFB1B3C1));
                iv_sports_riding.setImageResource(R.mipmap.ico_riding_gray);
            }
        });
        ll_sports_walk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                type = 1;
                ll_sports_run.setBackgroundResource(R.drawable.bg_ffeeeeee_360dp);
                tv_sports_run.setTextColor(CommonUtil.getColor(R.color.color_FFB1B3C1));
                iv_sports_run.setImageResource(R.mipmap.ico_run_gray);

                ll_sports_walk.setBackgroundResource(R.drawable.bg_ee4238_df2526_360dp);
                tv_sports_walk.setTextColor(CommonUtil.getColor(R.color.white));
                iv_sports_walk.setImageResource(R.mipmap.ico_walk_white);

                ll_sports_riding.setBackgroundResource(R.drawable.bg_ffeeeeee_360dp);
                tv_sports_riding.setTextColor(CommonUtil.getColor(R.color.color_FFB1B3C1));
                iv_sports_riding.setImageResource(R.mipmap.ico_riding_gray);
            }
        });
        ll_sports_riding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                type = 2;
                ll_sports_run.setBackgroundResource(R.drawable.bg_ffeeeeee_360dp);
                tv_sports_run.setTextColor(CommonUtil.getColor(R.color.color_FFB1B3C1));
                iv_sports_run.setImageResource(R.mipmap.ico_run_gray);

                ll_sports_walk.setBackgroundResource(R.drawable.bg_ffeeeeee_360dp);
                tv_sports_walk.setTextColor(CommonUtil.getColor(R.color.color_FFB1B3C1));
                iv_sports_walk.setImageResource(R.mipmap.ico_walk_gray);

                ll_sports_riding.setBackgroundResource(R.drawable.bg_ee4238_df2526_360dp);
                tv_sports_riding.setTextColor(CommonUtil.getColor(R.color.white));
                iv_sports_riding.setImageResource(R.mipmap.ico_riding_white);
            }
        });

        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CommonUtil.judgeNetIsConnected()) {
                    startInten(type);
                    dismiss();
                } else {
                    ToastUtils.showShort("请连接网络");
                }

            }
        });

    }

    @SuppressLint("WrongConstant")
    private void startInten(int position) {
        //请求动态权限
        PermissionUtils.permission(PermissionConstants.LOCATION,
                PermissionConstants.STORAGE,
                PermissionConstants.CAMERA,
                PermissionConstants.WRITE_EXTERNAL_STORAGE,
                PermissionConstants.ACCESS_COARSE_LOCATION)
                .rationale(new PermissionUtils.OnRationaleListener() {
                    @Override
                    public void rationale(final PermissionUtils.OnRationaleListener.ShouldRequest shouldRequest) {
                        DialogHelper.showRationaleDialog(shouldRequest);
                    }
                })
                .callback(new PermissionUtils.FullCallback() {
                    @Override
                    public void onGranted(List<String> permissionsGranted) {
                        Intent intent = new Intent(context, SportsStatusActivity.class);
                        intent.putExtra("SPORTS_TYPE", position == 1 ? 2 : position == 2 ? 1 : 0);
                        context.startActivity(intent);
                    }

                    @Override
                    public void onDenied(List<String> permissionsDeniedForever,
                                         List<String> permissionsDenied) {
                        if (!permissionsDeniedForever.isEmpty()) {
                            DialogHelper.showOpenAppSettingDialog();
                        }
                    }
                })
                .request();
    }

    public void show() {
//        this.setAnimationStyle(R.style.Animation);
        //防止部分手机底部状态栏与popupWindow重叠
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.setAnimationStyle(R.style.AnimationBottomFade);
        //popupWindow底部显示
        this.showAtLocation(((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content), Gravity.BOTTOM, 0, 0);
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();

        lp.alpha = 0.5f;
        if (lp.alpha == 1) {
            ((Activity) context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            ((Activity) context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        ((Activity) context).getWindow().setAttributes(lp);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = 1f;
        ((Activity) context).getWindow().setAttributes(lp);
    }

}
